<template>
  <div class="monitor-container">
    <!-- 返回按钮 -->
    <el-button type="text" class="back-btn" @click="$router.go(-1)">
      <el-icon><ArrowLeft /></el-icon> 返回首页
    </el-button>

    <!-- 任务基本信息 -->
    <el-descriptions title="模型监控面板" :column="2" border class="task-header">
      <el-descriptions-item label="任务名称">医疗影像分类</el-descriptions-item>
      <el-descriptions-item label="当前状态">
        <el-tag type="success">训练中</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="参与节点">142 个</el-descriptions-item>
      <el-descriptions-item label="训练轮次">第 15 轮</el-descriptions-item>
    </el-descriptions>

    <!-- 可视化图表 -->
    <el-row :gutter="20" class="chart-section">
      <el-col :xs="24" :sm="24" :md="12">
        <div class="chart-card">
          <h3>损失曲线</h3>
          <div id="lossChart" class="chart"></div>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12">
        <div class="chart-card">
          <h3>准确率曲线</h3>
          <div id="accuracyChart" class="chart"></div>
        </div>
      </el-col>
    </el-row>

    <!-- 统计信息 -->
    <el-row :gutter="20" class="stats">
      <el-col :xs="24" :sm="12" :md="6" v-for="stat in stats" :key="stat.title">
        <el-statistic
            :title="stat.title"
            :value="stat.value"
            :precision="stat.precision"
            :suffix="stat.suffix"
            class="stat-item"
        />
      </el-col>
    </el-row>

    <!-- 训练日志 -->
    <el-card class="log-section">
      <template #header>
        <div class="card-header">
          <span>实时训练日志</span>
          <el-button type="primary" text>导出日志</el-button>
        </div>
      </template>
      <div class="log-content">
        <div v-for="(log, index) in logs" :key="index" class="log-item">
          [{{ log.time }}] {{ log.message }}
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {onMounted, onBeforeUnmount, ref} from 'vue'
import * as echarts from 'echarts'
import {ArrowLeft} from "@element-plus/icons-vue";

// 初始化图表
let lossChart = null
let accuracyChart = null

// 静态数据
const stats = ref([
  { title: '当前轮次', value: 15, suffix: '轮' },
  { title: '参与节点', value: 142, suffix: '个' },
  { title: '平均损失', value: 0.32, suffix: '', precision: 2 },
  { title: '平均准确率', value: 89.5, suffix: '%', precision: 1 }
])

const logs = ref([
  { time: '09:30:15', message: '第15轮训练开始，接收全局模型参数' },
  { time: '09:32:45', message: '本地训练完成，损失值：0.34' },
  { time: '09:35:20', message: '参数上传成功，等待聚合' },
  { time: '09:38:10', message: '收到第15轮聚合模型' }
])

// 图表配置
const initCharts = () => {
  // 损失图表
  lossChart = echarts.init(document.getElementById('lossChart'))
  lossChart.setOption({
    title: { text: '' },
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: Array.from({length: 15}, (_, i) => `第${i+1}轮`)
    },
    yAxis: { type: 'value' },
    series: [{
      name: '损失值',
      type: 'line',
      smooth: true,
      data: Array.from({length: 15}, () => Math.random().toFixed(2)),
      itemStyle: { color: '#5470C6' }
    }]
  })

  // 准确率图表
  accuracyChart = echarts.init(document.getElementById('accuracyChart'))
  accuracyChart.setOption({
    title: { text: '' },
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: Array.from({length: 15}, (_, i) => `第${i+1}轮`)
    },
    yAxis: {
      type: 'value',
      axisLabel: { formatter: '{value} %' }
    },
    series: [{
      name: '准确率',
      type: 'line',
      smooth: true,
      data: Array.from({length: 15}, () => (Math.random() * 10 + 85).toFixed(1)),
      itemStyle: { color: '#91CC75' }
    }]
  })
}

onMounted(() => {
  initCharts()
})

onBeforeUnmount(() => {
  lossChart.dispose()
  accuracyChart.dispose()
})
</script>

<style scoped>
.monitor-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.back-btn {
  margin-bottom: 20px;
  color: #666;
  &:hover {
    color: #409eff;
  }
}

.chart-section {
  margin: 20px 0;
}

.chart-card {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  margin-bottom: 20px;

  h3 {
    margin-bottom: 15px;
    color: #333;
  }
}

.chart {
  height: 400px;
}

.stats {
  margin: 20px 0;
}

.stat-item {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.log-content {
  height: 300px;
  overflow-y: auto;
  background: #f8f9fa;
  padding: 15px;
  border-radius: 4px;
}

.log-item {
  padding: 8px 0;
  font-family: monospace;
  font-size: 14px;
  color: #666;
  border-bottom: 1px solid #eee;

  &:last-child {
    border-bottom: none;
  }
}

@media (max-width: 768px) {
  .chart {
    height: 300px;
  }
}
</style>